package Servlet.member;

import domain.News;
import service.NewsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/searches")
public class SearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置参数，默认为1
        Long newsTypeId = 1L;
        String result = request.getParameter("search");
        String reqTypeId=request.getParameter("reqNewsTypeId");

        List<News> newsList = null;



        // 当id值不为空时，将字符串id转换为Long数字类型
        if (reqTypeId != null) {
            newsTypeId = Long.parseLong(reqTypeId);
        }

        newsList = NewsService.getNewsContentByKeyWord(result);

//        System.out.println(pageCount);
        // 将newsList集合储存到域对象中
        request.setAttribute("newsList", newsList);
        request.setAttribute("newsTypeId", newsTypeId);

//        req.setAttribute( "newsList",newsList);
        request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request,response);




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
